package com.qire.manhua.model.viewModel.impl;

import android.graphics.Paint;
import android.view.View;

import com.qire.common.collect.ListUtil;
import com.qire.common.model.enumeration.VIPType;
import com.qire.common.support.SmartRefreshViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerRecyclerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.R;
import com.qire.manhua.controller.MessageLikeListController;
import com.qire.manhua.dal.service.MessageLikeListService;
import com.qire.manhua.databinding.LayoutMessageLikeItemBinding;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MessageLikeListViewModel extends SmartRefreshViewModel implements IViewModel {

    private MessageLikeListService messageLikeService = new MessageLikeListService(this);

    private final ArrayList<MessageLikeItemViewModel> messageLikeList = new ArrayList<>();
    private final SummerRecyclerBindAdapter messageLikeAdapter = new SummerRecyclerBindAdapter(messageLikeList);

    private int pageNo = 0;

    @Override
    public int getVariableId() {
        return BR.messageLikeListViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     */
    public void onAddition(MessageLikeListController controller) {
        messageLikeAdapter.setEventProcess(controller);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        this.messageLikeService = null;
        messageLikeAdapter.setEventProcess(null);
    }
    //endregion #附加管理 end


    //region    #业务数据 begin

    /**
     * 重置页面
     */
    public void resetPage() {
        pageNo = 0;
        clearMessageLikeList();
    }

    /**
     * 翻到下一页
     */
    public void nextPage() {
        pageNo++;
    }

    public void markAllRead() {
        messageLikeService.markAllRead();
    }

    public void loadData() {
        resetPage();
        messageLikeService.loadPageData(pageNo);
    }

    public void loadMoreData() {
        messageLikeService.loadPageData(pageNo);
    }

    /**
     * 评论列表项数据相关操作
     */
    private void clearMessageLikeList() {
        messageLikeAdapter.notifyItemRangeRemoved(0, messageLikeList.size());
        messageLikeList.clear();
    }
    public void addMessageLikeItem(int id, int comicId, String userName, String userPortrait, VIPType vipType,
                                   CharSequence quotedContent, boolean isQuotedDeleted, String comicName, String giveLikeTime) {
        messageLikeList.add(new MessageLikeItemViewModel(id, comicId, userName, userPortrait, vipType, quotedContent, isQuotedDeleted, comicName, giveLikeTime));
    }
    public void refreshMessageLikeAdapter() {
        messageLikeAdapter.notifyItemChanged(0, "full");
    }

    //endregion #业务数据 end


    //region    #UI操作 begin

    public void refreshUI() {
        refreshEmptyDataPanel();
    }

    public void refreshEmptyDataPanel() {
        boolean isEmpty = ListUtil.isEmpty(messageLikeList);
        set("dataPanelNo", isEmpty ? 0 : 1);
    }

    //endregion #UI操作 end

    //region    #UI绑定 begin

    public MutableLiveData<Integer> getDataPanelNo() {
        return get("dataPanelNo");
    }

    public RecyclerView.Adapter getMessageLikeAdapter() {
        return messageLikeAdapter;
    }

    public RecyclerView.LayoutManager getMessageLikeLayoutManager() {
        return new LinearLayoutManager(null);
    }

    //endregion #UI绑定 end

    @AdapterEntityBind(viewRid = R.layout.layout_message_like_item)
    public class MessageLikeItemViewModel extends BaseObservable implements AutoVariable<LayoutMessageLikeItemBinding> {

        public  final int id;
        public  final int comicId;
        private final String userName;
        private final String userPortrait;
        private final VIPType vipType;
        private final CharSequence quotedContent;
        private final boolean isQuotedDeleted;
        private final String comicName;
        private final String giveLikeTime;

        public MessageLikeItemViewModel(int id, int comicId, String userName, String userPortrait, VIPType vipType,
                                        CharSequence quotedContent, boolean isQuotedDeleted, String comicName, String giveLikeTime) {
            this.id = id;
            this.comicId = comicId;
            this.userName = userName;
            this.userPortrait = userPortrait;
            this.vipType = vipType;
            this.quotedContent = quotedContent;
            this.isQuotedDeleted = isQuotedDeleted;
            this.comicName = comicName;
            this.giveLikeTime = giveLikeTime;
        }

        @Override
        public void bindVariable(LayoutMessageLikeItemBinding viewBind) {
            viewBind.setMessageLikeItemViewModel(this);
        }

        @Bindable
        public int getPortraitHeadWearVisible() {
            return vipType == VIPType.VIP ? View.VISIBLE : View.GONE;
        }

        @Bindable
        public String getPortraitPath() {
            return userPortrait;
        }

        @Bindable
        public CharSequence getUserName() {
            return userName;
        }

        @Bindable
        public String getUserNameColor() {
            return vipType == VIPType.VIP ? "#ff5050" : "#666666";
        }

        @Bindable
        public CharSequence getQuotedContent() {
            return quotedContent;
        }

        @Bindable
        public int getQuotedContentPaintFlags() {
            return isQuotedDeleted ? Paint.STRIKE_THRU_TEXT_FLAG : -1;
        }

        @Bindable
        public CharSequence getSourceInfo() {
            return  String.format("来自漫画《%s》", comicName);
        }

        @Bindable
        public CharSequence getGiveLikeTime() {
            return giveLikeTime;
        }

    }


}
